﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RedSand.Site.Models;
using RedSand.Web.Mvc;

namespace RedSand.Site.Controllers
{
    public class RegistryController : BaseController
    {

        public ActionResult Manager()
        {
            ManagerModel model = new ManagerModel { Title = Translate("RegistryManager", "Registry"), AttachTinyMce = true };
            return View(model);
        }
        [AjaxOrChildAction]
        public ActionResult List()
        {
            return PartialView();
        }
        [HttpGet, AjaxOrChildAction]
        public ActionResult Edit(string key, string type, string title)
        {
            ViewBag.Title = title;
            RegistryInfo info = RsDataManager.GetInfo<RegistryInfo>("RegistryGetInfo", "Key", key);
            
            if (type == "string")
            {
                return PartialView("EditString", info);
            }
            if (type == "int")
            {
                return PartialView("EditInt", info);
            }
            return RedirectToAction("Index");
        }

        [HttpPost, AjaxOrChildAction]
        public ActionResult Edit(RegistryInfo info)
        {
            if (string.IsNullOrWhiteSpace(info.Value))
            {
                return AjaxMessage(Message.ValueEmpty);
            }
            info.Value = HtmlDecode(info.Value);
            if (RsDataManager.Save("RegistrySave", info))
            {
                AddMessage(Message.UpdateSuccess);
                return RedirectToAction("List");
            }
            return AjaxMessage(Message.UpdateError);
        }

        [HttpGet, AjaxOrChildAction]
        public ActionResult IntroEdit()
        {
            ViewBag.Description = GetValue("Intro-Description");
            ViewBag.Content = GetValue("Intro-Content");
            ViewBag.Image = GetValue("Intro-Image");
            return PartialView();
        }

        [HttpPost, AjaxOrChildAction]
        public ActionResult IntroEdit(string Description, string Content, string NewImage, string OldImage)
        {
            if (string.IsNullOrWhiteSpace(NewImage) && string.IsNullOrWhiteSpace(OldImage))
            {
                return AjaxMessage(Message.ImageUrlEmpty);
            }
            if (string.IsNullOrWhiteSpace(Description))
            {
                return AjaxMessage(Message.DescriptionEmpty);
            }
            if (string.IsNullOrWhiteSpace(Content))
            {
                return AjaxMessage(Message.ContentEmpty);
            }
            SetValue("Intro-Description", HtmlDecode(Description));
            SetValue("Intro-Content", HtmlDecode(Content));
            SetValue("Intro-Image", SaveImage(OldImage));
            return RedirectToAction("List");
        }

        [HttpGet, AjaxOrChildAction]
        public string GetValue(string key)
        {
            RegistryInfo info=RsDataManager.GetInfo<RegistryInfo>("RegistryGetInfo", "Key", key);
            return info != null ? info.Value : null;
            
        }

        [HttpGet, AjaxOrChildAction]
        public bool SetValue(string key, string value)
        {
            RegistryInfo info = new RegistryInfo();
            info.Key=key;
            info.Value=value;
            return RsDataManager.Save("RegistrySave", info);
        }

        [HttpGet, AjaxOrChildAction]
        public string GetVisitorCount()
        {
            string value = GetValue("VisitorCount");
            if (Session["SetOnlineNumber"] == null)
            {
                value = (Convert.ToInt32(value) + 1).ToString();
                SetValue("VisitorCount", value);
                Session["SetOnlineNumber"] = true;
            }
            return value;
        }
    }
}
